package com.zking.controller;

import com.zking.entity.Product;
import com.zking.entity.Users;
import com.zking.service.AddressService;
import com.zking.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    @Autowired
    AddressService addressService;
    @Autowired
    ProductService productService;


    // 本地址需要配置拦截器 先效验是否登录
    @RequestMapping("todingdan")
    public String todd(HttpSession session, String yanse,
                       String chima, Integer count, Model m){
        // 从session中取出登录的用户
        Users users = (Users) session.getAttribute("users");
        // \\\\\  模拟登录用户  //////
        users = new Users(15,"user","小明","123","男","18899990000",null,null,null,null);
        // 取出要购买的商品id 加入集合
        Integer spid = (Integer) session.getAttribute("productid");
        Product product = productService.selectById(spid);
        List<Product> list = new ArrayList<>();
        list.add(product);
        // 保存订单详情页需要的数据
        m.addAttribute("address",addressService.findByUid(users.getId()));
        m.addAttribute("yanse",yanse);
        m.addAttribute("chima",chima);
        m.addAttribute("products",list);
        m.addAttribute("count",count);
        // 计算总金额
        DecimalFormat df = new DecimalFormat("#.##");
        m.addAttribute("money",df.format(count*product.getPrice()));
        return "tij_dingd";
    }
}
